<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OpenMesh: Some basic operations: Flipping and collapsing edges</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="logo_align.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="rwth_vci_rgb.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">OpenMesh
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('a04331.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Some basic operations: Flipping and collapsing edges </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>In this section you will learn about some basic operations on a mesh that OpenMesh already provides.</p>
<p>Comprising the flipping of edges in a triangle mesh as well as collapsing edges by joining the two adjacent vertices.</p>
<ul>
<li><a class="el" href="a04331.html#op_flip">Flipping edges in triangle meshes</a> </li>
<li><a class="el" href="a04331.html#op_collapse">Collapsing edges</a></li>
</ul>
<h1><a class="anchor" id="op_flip"></a>
Flipping edges in triangle meshes</h1>
<p>Considering two adjacent faces of a triangle mesh, there exist exactly two different configurations of the inner edge. Calling the function <a class="el" href="a02481.html#a03b3c3be3c52a194789bd541d24f4c09" title="Flip edge _eh. ">OpenMesh::TriConnectivity::flip(EdgeHandle _eh)</a> will flip the specified edge to its opposite orientation as shown in the illustration below.</p>
<div class="image">
<img src="mesh.flip.png" alt="mesh.flip.png"/>
<div class="caption">
Flipping edges in a triangle mesh</div></div>
<p> So, the following snippet of code shows how to use this in your applications:</p>
<div class="fragment"><div class="line">TriMesh mesh;</div><div class="line"></div><div class="line"><span class="comment">// Add some vertices</span></div><div class="line">TriMesh::VertexHandle vhandle[4];</div><div class="line"></div><div class="line">vhandle[0] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(0, 0, 0));</div><div class="line">vhandle[1] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(0, 1, 0));</div><div class="line">vhandle[2] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(1, 1, 0));</div><div class="line">vhandle[3] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(1, 0, 0));</div><div class="line"></div><div class="line"><span class="comment">// Add two faces</span></div><div class="line">std::vector&lt;TriMesh::VertexHandle&gt; face_vhandles;</div><div class="line"></div><div class="line">face_vhandles.push_back(vhandle[2]);</div><div class="line">face_vhandles.push_back(vhandle[1]);</div><div class="line">face_vhandles.push_back(vhandle[0]);</div><div class="line">mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">face_vhandles.clear();</div><div class="line"></div><div class="line">face_vhandles.push_back(vhandle[2]);</div><div class="line">face_vhandles.push_back(vhandle[0]);</div><div class="line">face_vhandles.push_back(vhandle[3]);</div><div class="line">mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line"><span class="comment">// Now the edge adjacent to the two faces connects</span></div><div class="line"><span class="comment">// vertex vhandle[0] and vhandle[2].</span></div><div class="line"></div><div class="line"><span class="comment">// Find this edge and then flip it</span></div><div class="line"><span class="keywordflow">for</span>(TriMesh::EdgeIter it = mesh.edges_begin(); it != mesh.edges_end(); ++it) {</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>(!mesh.is_boundary(*it)) {</div><div class="line">                <span class="comment">// Flip edge</span></div><div class="line">                mesh.flip(*it);</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// The edge now connects vertex vhandle[1] and vhandle[3].</span></div></div><!-- fragment --><h1><a class="anchor" id="op_collapse"></a>
Collapsing edges</h1>
<p>In this section you will learn how to collapse edges such that the two adjacent vertices join. OpenMesh provides the function <a class="el" href="a02313.html#a5afce0f6be1f425f5e0d42441fee35df" title="Halfedge collapse: collapse the from-vertex of halfedge _heh into its to-vertex. ">OpenMesh::PolyConnectivity::collapse(HalfedgeHandle _heh)</a> to perform this operation. This will collapse the from-vertex (remeber that halfedges are directed) to the to-vertex of the halfedge as illustrated below. Note that collapsing edges might cause topological inconsistencies to your mesh. You should verify consistency after collapsing edges by calling <a class="el" href="a02313.html#af1463136f43f48a7e6576701375d018c" title="Returns whether collapsing halfedge _heh is ok or would lead to topological inconsistencies. ">OpenMesh::PolyConnectivity::is_collapse_ok()</a>.</p>
<dl class="section note"><dt>Note</dt><dd>You have to request status attributes in order to use the collapse and delete functions!</dd></dl>
<div class="image">
<img src="mesh.collapse.png" alt="mesh.collapse.png"/>
<div class="caption">
Collapsing will always be performed in the direction the halfedge points to.</div></div>
<p> A simple code example related to the illustration might look like this:</p>
<div class="fragment"><div class="line">PolyMesh mesh;</div><div class="line"></div><div class="line"><span class="comment">// Request required status flags</span></div><div class="line">mesh.request_vertex_status();</div><div class="line">mesh.request_edge_status();</div><div class="line">mesh.request_face_status();</div><div class="line"></div><div class="line"><span class="comment">// Add some vertices as in the illustration above</span></div><div class="line"><a class="code" href="a02389.html#a0fc18e13d70825938274e3faf7b55e01">PolyMesh::VertexHandle</a> vhandle[7];</div><div class="line">vhandle[0] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, 1, 0));</div><div class="line">vhandle[1] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, 3, 0));</div><div class="line">vhandle[2] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(0, 0, 0));</div><div class="line">vhandle[3] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(0, 2, 0));</div><div class="line">vhandle[4] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(0, 4, 0));</div><div class="line">vhandle[5] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(1, 1, 0));</div><div class="line">vhandle[6] = mesh.add_vertex(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(1, 3, 0));</div><div class="line"></div><div class="line"><span class="comment">// Add three quad faces</span></div><div class="line">std::vector&lt;PolyMesh::VertexHandle&gt; face_vhandles;</div><div class="line">face_vhandles.push_back(vhandle[1]);</div><div class="line">face_vhandles.push_back(vhandle[0]);</div><div class="line">face_vhandles.push_back(vhandle[2]);</div><div class="line">face_vhandles.push_back(vhandle[3]);</div><div class="line">mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">face_vhandles.clear();</div><div class="line">face_vhandles.push_back(vhandle[1]);</div><div class="line">face_vhandles.push_back(vhandle[3]);</div><div class="line">face_vhandles.push_back(vhandle[5]);</div><div class="line">face_vhandles.push_back(vhandle[4]);</div><div class="line">mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line">face_vhandles.clear();</div><div class="line">face_vhandles.push_back(vhandle[3]);</div><div class="line">face_vhandles.push_back(vhandle[2]);</div><div class="line">face_vhandles.push_back(vhandle[6]);</div><div class="line">face_vhandles.push_back(vhandle[5]);</div><div class="line">mesh.add_face(face_vhandles);</div><div class="line"></div><div class="line"><span class="comment">// Now find the edge between vertex vhandle[2]</span></div><div class="line"><span class="comment">// and vhandle[3]</span></div><div class="line"><span class="keywordflow">for</span>(PolyMesh::HalfedgeIter it = mesh.halfedges_begin(); it != mesh.halfedges_end(); ++it) {</div><div class="line">  <span class="keywordflow">if</span>( mesh.to_vertex_handle(*it) == vhandle[3] &amp;&amp;</div><div class="line">      mesh.from_vertex_handle(*it) == vhandle[2])</div><div class="line">  {</div><div class="line">    <span class="comment">// Collapse edge</span></div><div class="line">    mesh.collapse(*it);</div><div class="line">    <span class="keywordflow">break</span>;</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Our mesh now looks like in the illustration above after the collapsing.</span></div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr>
<address>
<small>
<a href="http://www.rwth-graphics.de" style="text-decoration:none;">
</a>
Project <b>OpenMesh</b>, 
&copy;&nbsp; Computer Graphics Group, RWTH Aachen.
Documentation generated using 
<a class="el" href="http://www.doxygen.org/index.html">
<b>doxygen</b>
</a>.
</small>
</address>
</body>
</html>
